package com.kuaimacode.kframework.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.coupon.UserCouponMapper;
import com.kuaimacode.kframework.mybatis.models.coupon.UserCoupon;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户优惠券表 服务类
 * </p>
 *
 * @author Bean
 * @since 2019-03-15
 */
@Service
public class UserCouponService extends ServiceImpl<UserCouponMapper, UserCoupon> {

    /**
     * 根据用户id 查询所有优惠券
     * @param userId
     * @return
     */
    public List<UserCoupon> queryAllCoupons(Long userId){
        QueryWrapper<UserCoupon> queryWrapper = new QueryWrapper();

        queryWrapper.eq("user_id",userId);
        queryWrapper.ne("state", Constant.COUPON_STATUS_DEL);

        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据优惠券状态查询 0  未使用 1 已使用  2 已过期 3 已删除
     * @param userId
     * @return
     */
    public PageInfo<UserCoupon> queryByState(int pageNum, int pageSize, Long userId, String status){
        PageHelper.startPage(pageNum, pageSize,false);

        List<UserCoupon> datas = baseMapper.queryUserCouponInfo(userId,status);
        return new PageInfo<UserCoupon>(datas);
    }

    /**
     * 查询用户下各个状态的优惠券的数量
     * @param userId
     * @param status
     * @return
     */
    public Integer queryStatusCount(Long userId,String status){
        QueryWrapper<UserCoupon> queryWrapper = new QueryWrapper();

        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("state",status);
        return baseMapper.selectCount(queryWrapper);

    }

    /**
     * 根据用户id ,优惠券id查询未使用的优惠券
     * @param userId
     * @return
     */
    public Map queryUserCouponInfoById(Long userId, Long couponId, Integer useScope){
        return baseMapper.queryUserCouponInfoById(userId, Constant.COUPON_STATUS_NOT_USED, couponId, useScope);
    }

	
}
